# @File    : cancel_ota.py
# @Project : Airtest_JBL
from common.log_util import info
from page.kill_ota import KillOta
from airtest.core.api import *


class CancelOta(KillOta):

    def ota_cancel(self):
        i = 1
        num = int(input("请输入压测次数:"))
        while i < num+1:
            version = self.check_version()
            if version == self.l_version:
                self.is_low(version)
                self.check_upgrade(i)
                version = self.poco(textMatches=f"{self.earbud}").get_text()[5:]
                if version == self.h_version:
                    i -= 1
            elif version == self.h_version:
                KillOta.is_height(self, i, version)
                i -= 1
            else:
                i -= 1
            if i < num:
                sleep(1)
                stop_app("jbl.stc.com.debug")
                sleep(2)
                start_app("jbl.stc.com.debug")
                sleep(1)
            i += 1
        info(f"取消升级测试完成, 共压测 {i-1} 次, 其中 PASS {self.pass_num} 次, FAIL {self.fail_num} 次, FAIL的轮数为{self.fail_list}\n")

    def check_upgrade(self, i):
        while True:
            if self.poco(text="重试").exists():
                self.poco(text="重试").click()
                sleep(1)
                self.poco(text="更新").click()
            if self.poco(textMatches=f"{self.surplus}").exists():
                result = self.is_install(self.poco(textMatches=f"{self.process}").get_text())
                if result:
                    while True:
                        if self.poco(text="更新成功").exists():
                            self.poco(text="完成").click()
                            break
                    break
                else:
                    self.poco(text="取消").click()
                    self.poco(text="退出").click()
                    version = self.poco(textMatches=f"{self.earbud}").get_text()[5:]
                    if version == self.l_version:
                        info(f"取消升级后耳机版本为: {version}")
                        info(f"第 {i} 次测试结果为: PASS")
                        self.pass_num += 1
                    else:
                        info(f"取消升级后耳机版本为: {version}")
                        info(f"第 {i} 次测试结果为: FAIL")
                        self.fail_num += 1
                        self.fail_list.append(i)
                    break





